package com.lw.udpdemo;

import javax.net.ssl.HttpsURLConnection;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

/**
 * @author LW
 * @2020/9/19 13:28
 */
//知道url 下载文件
public class URLDown {

    public static void main(String[] args) throws Exception {

        //1.下载地址
        URL url = new URL("https://p2.music.126.net/0O1DSaOFQDa0XAL1oxBDfg==/109951165325140747.jpg?param=34y34");
        //2.连接到这个资源，   HTTP(其实是协议的单词缩写，不是一个单词)
        //地址强转
        HttpsURLConnection urlConnection = (HttpsURLConnection) url.openConnection();

        InputStream inputStream = urlConnection.getInputStream();
        FileOutputStream fos = new FileOutputStream("109951165325140747.jpg");

        byte[] buffer = new byte[1024];
        int len;
        while ((len=inputStream.read(buffer))!=-1){
            fos.write(buffer,0,len);//写出这个数据
        }

        fos.close();
        inputStream.close();
        urlConnection.disconnect();//断开



    }
}
